package com.szqhyxj.youxiangju.biz.merchantHome.shop.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.OrderDeetails;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.adapter.OrderAdapter;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订单列表
 */
public class OrderListActivity extends BaseActivity implements NavigationBar.NavigationBarListener, OrderContract.View, OrderAdapter.OnClickitem, MsgChoicePopView.OnClickListener{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.list_order)
    ListView listOrder;
    @BindView(R.id.no_site_prompt)
    LinearLayout noSitePrompt;
    private List<OrderDeetails> deetails = new ArrayList<>();
    private OrderAdapter adapter;
    private OrderPresenter presenter;
    private int vernier;
    private MsgChoicePopView choicePopView;

    public static void start(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity, OrderListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        adapter = new OrderAdapter(deetails, this);
        listOrder.setAdapter(adapter);
        presenter = new OrderPresenter(this);
        initEvent();
    }

    private void initEvent(){
        listOrder.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                navOrderDetails(deetails.get(i).getPayOrderId());
            }
        });
    }

    @Override
    protected void onResume(){
        super.onResume();
        presenter.init();
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }

    @Override
    public void showOrderList(List<OrderDeetails> list){
        listOrder.setVisibility(View.VISIBLE);
        this.deetails.clear();
        deetails.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showConfirmOrder(int i){
        deetails.get(i).setReceiverStatus("1");
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showNoOrder(){
        noSitePrompt.setVisibility(View.VISIBLE);
    }

    @Override
    public void navOrderDetails(String orderId){
        OrderDetailsActivity.start(this, orderId);
    }

    @Override
    public void onBuyNow(int i){
        this.vernier = i;

            choicePopView = new MsgChoicePopView(this, "请确定是否已收到货?");
            choicePopView.setOnClickListener(this);
            choicePopView.show();

    }

    @Override
    public void onClickConfirm(View v){
        choicePopView.dismiss();
        choicePopView = null;
        presenter.nowConfirm(vernier, deetails.get(vernier).getPayOrderId());
    }
}
